const Service = require("egg").Service;

class AlarmHistoryService extends Service {


    //设备列表
    async index(payload) {

        const { page=1, pageSize=10 } = payload

        const limit = parseInt( pageSize )

        const offset = (page - 1) * limit

        const lists = await this.ctx.model.THistorywarn.findAndCountAll({
            limit,
            offset,
        })

        return lists
    }

   
    //显示设备详细信息
    async show(id) {

        const device = await  this.ctx.model.THistorywarn.findByPk(id)

        return device;
    }

    //添加设备
    async add(payload) {

        const device = await  this.ctx.model.THistorywarn.create(payload)

        return device;
    }

    //更新设备列表
    async update(id, payload) {

        const device = await  this.ctx.model.THistorywarn.findByPk(id)

        if (!device) {
            this.ctx.throw(404, "not found device");
        }

        await device.update(payload);

        return device;
    }

    //删除设备
    async destroy(id) {

        const device = await  this.ctx.model.THistorywarn.findByPk(id)

        if (!device) {
            this.ctx.throw(404, "not found device");
        }

        return await device.destroy();
    }


}

module.exports = AlarmHistoryService